package cn.com.bs.srmp.util;

import java.util.Collection;
import java.util.Map;

import cn.com.bs.srmp.common.Constants;

/**
 * @Author mic
 * @Date 2022-08-02 17:47
 */
public class PluginUtil {

    /**
     * 从完整方法信息中获取方法名
     *
     * @param fullMethod 完整方法信息
     * @return
     */
    public static String getOnlyMethodName(String fullMethod) {
        int indexColon = fullMethod.indexOf(Constants.FLAG_COLON);
        int indexLeftBrackets = fullMethod.indexOf(Constants.FLAG_LEFT_BRACKET);
        return fullMethod.substring(indexColon + 1, indexLeftBrackets);
    }


    /**
     * 从完整方法信息中获取完整类名
     *
     * @param method 完整方法信息
     * @return
     */
    public static String getFullClassNameFromMethod(String method) {
        int indexLastColon = method.lastIndexOf(Constants.FLAG_COLON);
        return method.substring(0, indexLastColon);
    }


    /**
     * 从完整方法信息中获取参数带括号
     *
     * @param method 获取参数带括号
     * @return
     */
    public static String getParamsFromMethod(String method) {
        int indexLastColon = method.lastIndexOf(Constants.FLAG_LEFT_BRACKET);
        return method.substring(indexLastColon);
    }

    public static boolean isCollectionEmpty(Collection collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isMapEmpty(Map map) {
        return map == null || map.isEmpty();
    }

    /**
     * 获取当前目录的上级目录
     *
     * @param path
     * @return
     */
    public static String getParentDirectory(String path) {
        String parentDirectory = path;
        if (path.endsWith(Constants.FLAG_SLASH)) {
            parentDirectory = path.substring(0, path.length() - 1);
        }
        parentDirectory = parentDirectory.substring(0, parentDirectory.lastIndexOf(Constants.FLAG_SLASH));
        return parentDirectory + Constants.FLAG_SLASH;
    }


}
